package com.hunttown.mes.impl;

import com.hunttown.mes.common.utils.RegexpUtils;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportOperateLogRpcService;
import com.hunttown.mes.rpc.domain.TransportOperateLogDTO;
import com.hunttown.mes.rpc.domain.query.TransportOperateLogDTOQuery;
import com.hunttown.mes.service.TransportOperateLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportOperateLogConverts.convert;

/**
 * created by wangjunfu on 2019-05-19 01:20:14
 */
@Service
public class TransportOperateLogRpcServiceImpl implements TransportOperateLogRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportOperateLogRpcServiceImpl.class);

    private final TransportOperateLogService transportOperateLogService;

    @Autowired
    public TransportOperateLogRpcServiceImpl(TransportOperateLogService transportOperateLogService) {
        this.transportOperateLogService = transportOperateLogService;
    }

    @Override
    public TransportOperateLogDTO insert(TransportOperateLogDTO transportOperateLogDTO) {
        try {
            return convert(transportOperateLogService.insert(convert(transportOperateLogDTO)));
        } catch (Exception e) {
            logger.error("TransportOperateLog create failed.", e);
            throw new RuntimeException("TransportOperateLog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportOperateLogDTO transportOperateLogDTO) {
        try {
            return transportOperateLogService.updateInfoById(convert(transportOperateLogDTO));
        } catch (Exception e) {
            logger.error("TransportOperateLog update by id failed.", e);
            throw new RuntimeException("TransportOperateLog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportOperateLogDTOQuery transportOperateLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportOperateLogDTOQuery.getW_id() != null && transportOperateLogDTOQuery.getW_id() > 0) {
                query.put("w_id", transportOperateLogDTOQuery.getW_id());
            }

            if (transportOperateLogDTOQuery.getIds() != null && !transportOperateLogDTOQuery.getIds().equals("")) {
                query.put("ids", transportOperateLogDTOQuery.getIds());
            }

            return transportOperateLogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportOperateLog update by query failed.", e);
            throw new RuntimeException("TransportOperateLog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportOperateLogDTO selectById(Integer id) {
        try {
            return convert(transportOperateLogService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportOperateLog select by id failed.", e);
            throw new RuntimeException("TransportOperateLog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportOperateLogDTO selectObjByQuery(TransportOperateLogDTOQuery transportOperateLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(transportOperateLogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportOperateLog select obj by query failed.", e);
            throw new RuntimeException("TransportOperateLog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportOperateLogDTO selectObjWithoutEnt(TransportOperateLogDTOQuery transportOperateLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportOperateLogDTOQuery.getTableId() != null) {
                query.put("tableId", transportOperateLogDTOQuery.getTableId());
            }

            if (StringUtils.isNotBlank(transportOperateLogDTOQuery.getShowDate())) {
                query.put("showDate", transportOperateLogDTOQuery.getShowDate());
            }

            if (StringUtils.isNotBlank(transportOperateLogDTOQuery.getStartDate())) {
                query.put("startDate", transportOperateLogDTOQuery.getStartDate());
            }

            if (StringUtils.isNotBlank(transportOperateLogDTOQuery.getEndDate())) {
                query.put("endDate", transportOperateLogDTOQuery.getEndDate());
            }

            return convert(transportOperateLogService.selectObjWithoutEnt(query));
        } catch (Exception e) {
            logger.error("TransportOperateLog select obj without ent failed.", e);
            throw new RuntimeException("TransportOperateLog select obj without ent failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportOperateLogDTO> getListForPage(TransportOperateLogDTOQuery transportOperateLogDTOQuery) {
        Query query = Query.page(transportOperateLogDTOQuery.getPn(), transportOperateLogDTOQuery.getPageSize());

        if (transportOperateLogDTOQuery.getId() != null && transportOperateLogDTOQuery.getId() > 0) {
            query.put("id", transportOperateLogDTOQuery.getId());
        }

        if (transportOperateLogDTOQuery.getIds() != null && !transportOperateLogDTOQuery.getIds().equals("")) {
            query.put("ids", transportOperateLogDTOQuery.getIds());
        }

        if (transportOperateLogDTOQuery.getTableId() != null && transportOperateLogDTOQuery.getTableId() > 0) {
            query.put("tableId", transportOperateLogDTOQuery.getTableId());
        }

        if (transportOperateLogDTOQuery.getRecordId() != null) {
            // 判断是否是数字
            if (RegexpUtils.isInteger(transportOperateLogDTOQuery.getRecordId().toString())) {
                query.put("recordId", transportOperateLogDTOQuery.getRecordId());
            }
        }

        if (transportOperateLogDTOQuery.getOpPeople() != null && transportOperateLogDTOQuery.getOpPeople() > 0) {
            query.put("opPeople", transportOperateLogDTOQuery.getOpPeople());
        }

        if (transportOperateLogDTOQuery.getModelType() != null && transportOperateLogDTOQuery.getModelType() > 0) {
            query.put("modelType", transportOperateLogDTOQuery.getModelType());
        }

        if (StringUtils.isNotBlank(transportOperateLogDTOQuery.getLikeName())) {
            query.put("likeName", transportOperateLogDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportOperateLogDTOQuery.getShowDate())) {
            query.put("showDate", transportOperateLogDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportOperateLogDTOQuery.getStartDate())) {
            query.put("startDate", transportOperateLogDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportOperateLogDTOQuery.getEndDate())) {
            query.put("endDate", transportOperateLogDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportOperateLogDTOQuery.getOrderBy())) {
            query.put("orderBy", transportOperateLogDTOQuery.getOrderBy());
        }

        //区间日期
        if (StringUtils.isNotBlank(transportOperateLogDTOQuery.getDaterange())) {
            String[] date = transportOperateLogDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        List<TransportOperateLogDTO> list = convert(transportOperateLogService.selectByQuery(query));
        Page<TransportOperateLogDTO> page = Page.from(transportOperateLogDTOQuery.getPn(), transportOperateLogDTOQuery.getPageSize(), list);
        page.setTotalItems(transportOperateLogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportOperateLogService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportOperateLog delete failed.", e);
            throw new RuntimeException("TransportOperateLog delete failed, Please order your code.", e);
        }
    }
}